<template>
  <el-dialog
    title="平台给供应商充值（积分日元1：1）"
    destroy-on-close
    append-to-body
    :visible.sync="show"
    width="600px"
    :before-close="cancel">
    <el-form ref="formRef" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="充值点数" prop="amount">
        <el-input type="number" v-model.number="form.amount" />
      </el-form-item>
      <el-form-item label="充值理由" prop="remark">
        <el-input v-model="form.remark" type="textarea" :rows="7" show-word-limit maxlength="500" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleRecharge">确认 </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addBalance } from '@/api/bill/platDistributerBalance'

export default {
  activated() {
    this.$nextTick(() => this.$refs['TableRef']?.doLayout())
  },
  props: {
    /**
     * data
     */
    formData: {
      type: Object,
      default: () => ({}),
    },
    /**
     * 控制显示隐藏
     */
    show: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      queryParams: {},
      form: {
        amount: null,
        remark: '',
      },
      // 表单校验
      rules: {
        remark: [{ required: true, message: '请输入', trigger: 'blur' }],
        amount: [{ required: true, message: '请输入', trigger: 'blur' }],
      },
    }
  },
  watch: {
    show: function (newValue) {
      if (newValue) {
        Object.keys(this.form).forEach((key) => (this.form[key] = ''))
      }
    },
  },
  methods: {
    handleRecharge() {
      this.$refs['formRef'].validate((valid) => {
        if (valid) {
          const data = {
            ...this.form,
            id: this.formData.id,
          }
          addBalance(data).then((res) => {
            const { code, data, msg } = res
            if (code === 200) {
              this.$message({ type: 'success', message: msg })
              this.$emit('getList')
              this.cancel()
            }
          })
        }
      })
    },

    cancel() {
      this.$emit('update:show', false)
    },
  },
}
</script>
<style scoped lang="scss"></style>
